<?php

namespace app\common\model;

use think\Model;
use think\facade\Cache;
use think\model\concern\SoftDelete;

class SiteSetting extends Model
{
    /**
     * Use Thinkphp ORM Model
     */
    protected $autoWriteTimestamp = 'int';

    /**
     * 获取站点配置信息
     * @params string
     * @return
     */
    public static function getSite()
    {
        $info = Cache::get('site');
        if(empty($info))
        {
            $info = self::where('id', 1)->field('site_name,logo,keyword,describe,copyright,site_icp,site_status')
                ->findOrEmpty()->toArray();
            if(!empty($info))
            {
                Cache::set('site', $info);
            }
        }
        return $info;
    }
    /**
     * 更改站点配置
     * @params string
     * @return
     */
    public static function editSite($data = [])
    {
        $res = self::update($data, ['id'=>1]);
        if($res)
        {
            Cache::delete('site');
        }
        Cache::set('site', $data);
        return $res;
    }

    /**
     * 获取上传文件配置
     * @params string
     * @return
     */
    public static function getUploadSetting()
    {
        $info = Cache::get('upload_setting');
        if(empty($info))
        {
            $info = self::where('id', 1)->field('upload_path,file_ext,upload_img_size,upload_file_size,delete_local,path_info')
                ->findOrEmpty()->toArray();
            if(!empty($info))
            {
                $info['path_info'] = json_decode($info['path_info'], true);
                Cache::set('upload_setting', $info);
            }
        }
        if(!is_array($info['path_info']))
        {
            $info['path_info'] = json_decode($info['path_info'], true);
        }
        return $info;
    }

    /**
     * 修改文件上传配置
     * @params string
     * @return
     */
    public static function editUploadSetting($data = [])
    {
        $data['path_info'] = json_encode($data['path_info']);

        $res = self::update($data, ['id'=>1]);
        if($res)
        {
            Cache::delete('upload_setting');
        }
        Cache::set('upload_setting', $data);
        return $res;
    }
}